package org.apache.xmlrpc.webserver;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;

public class Calculator {
	public int add(int i1, int i2) {
		return i1 + i2;
	}

	public int insert(int i1) {
		int result = -999;

		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/MyDataSource");
			java.sql.Connection conn = ds.getConnection();

			Statement st = conn.createStatement();
			st.executeUpdate("INSERT INTO EMP(NUM) VALUES (" + i1 + ")");
			ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM EMP");
			while (rs.next()) {
				result = rs.getInt(1);
				System.out.println("EMP = " + result);
			}
			// results = sql.executeQuery();
		} catch (NamingException e) {
			System.err.println(e);
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}
		return result;
	}

	public int insert_group(String groupname, int level) {
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/MyDataSource");
			java.sql.Connection conn = ds.getConnection();

			Statement st = conn.createStatement();
			st.executeUpdate("INSERT INTO sysusage_group VALUES ('" + groupname
					+ "'," + level + ")");
		} catch (NamingException e) {
			System.err.println(e);
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}
		return 999;
	}

	public int insert_server(String hostname, String groupname,
			String servertype) {
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/MyDataSource");
			java.sql.Connection conn = ds.getConnection();

			Statement st = conn.createStatement();
			st.executeUpdate("INSERT INTO sysusage_server VALUES ('" + hostname
					+ "','" + groupname + "','" + servertype + "')");
		} catch (NamingException e) {
			System.err.println(e);
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}
		return 999;
	}

	public int insert_rawdata(String hostname, String property, int value) {
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/MyDataSource");
			java.sql.Connection conn = ds.getConnection();

			Statement st = conn.createStatement();
			st
					.executeUpdate("INSERT INTO sysusage_rawdata VALUES (CURRENT_TIMESTAMP, '"
							+ hostname + "','" + property + "'," + value + ")");
		} catch (NamingException e) {
			System.err.println(e);
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}
		return 999;
	}
}
